
package x.trident.core.log.starter;

import cn.hutool.system.SystemUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import x.trident.core.log.api.expander.LogConfigExpander;
import x.trident.core.log.api.pojo.log.SysLogProperties;
import x.trident.core.log.api.threadpool.LogManagerThreadPool;
import x.trident.core.log.file.FileLogRecordServiceImpl;
import x.trident.core.log.requestapi.RequestApiLogRecordAop;

/**
 * 系统日志的自动配置
 *
 * @author 林选伟
 * @date 2020/12/1 17:12
 */
@Configuration
public class TridentLogAutoConfiguration {

    /**
     * 日志配置的前缀
     */
    public static final String SYS_LOG_PREFIX = "sys-log";

    /**
     * 系统日志的配置
     */
    @Bean
    @ConfigurationProperties(prefix = SYS_LOG_PREFIX)
    public SysLogProperties sysLogProperties() {
        return new SysLogProperties();
    }

    /**
     * 每个请求接口记录日志的AOP
     * 根据配置文件初始化日志记录器
     * 日志存储类型：db-数据库，file-文件，默认存储在数据库中
     *
     * @param sysLogProperties 系统日志配置文件
     */
    @Bean
    public RequestApiLogRecordAop requestApiLogRecordAop(SysLogProperties sysLogProperties) {
        // 修改为从sys_config中获取日志存储位置
        String fileSavePath = "";
        if (SystemUtil.getOsInfo().isWindows()) {
            fileSavePath = LogConfigExpander.getLogFileSavePathWindows();
        } else {
            fileSavePath = LogConfigExpander.getLogFileSavePathLinux();
        }

        return new RequestApiLogRecordAop(new FileLogRecordServiceImpl(fileSavePath, new LogManagerThreadPool()));
    }

}


